<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/6/13
 * Time: 19:07
 */
namespace app\common\util\weixin;
class Request
{
    private $_requestData;

    public function __construct()
    {
        $xml = (array)simplexml_load_string(file_get_contents('php://input'), 'SimpleXMLElement', LIBXML_NOCDATA);
        //将数组键名转换为小写
        $this->_requestData = array_change_key_case($xml, CASE_LOWER);
    }

    public static function checkSignature($token)
    {
        $signature = I("get.signature", '', 'trim');
        $timestamp = I("get.timestamp", '', 'trim');
        $nonce = I("get.nonce", '', 'trim');
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);
        if ($tmpStr == $signature) {
            return true;
        }
        return false;
    }

    public function get($key, $defaultValue = null)
    {
        return isset($this->_requestData[$key]) ? $this->_requestData[$key] : $defaultValue;
    }

    public function set($key, $value)
    {
        $this->_requestData[$key] = $value;
    }

    public function __get($name)
    {
        // TODO: Implement __get() method.
        return $this->get($name);
    }

    public function __set($name, $value)
    {
        // TODO: Implement __set() method.
        $this->set($name, $value);
    }

}